import React, { useEffect, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { Card, Form, Input, Button, message } from "antd";
import type {
  getHosSetListItemType,
  addHospitalParamType,
} from "@api/hospital/model/hospitalSetTypes";
import {
  addHosSetFormRequest,
  updateHosSetFormRequest,
  getHosSetFormRequest,
} from "@api/hospital/hospitalSet";
import style from "../index.module.css";
export default function AddOfUpdateHosSet() {
  const navigate = useNavigate();
  const { id } = useParams();
  const [form] = Form.useForm();
  useEffect(() => {
    const getHosDetail = async () => {
      const re = await getHosSetFormRequest(id);
      form.setFieldsValue(re);
    };
    getHosDetail();
  }, []);
  //表单渲染
  const onFinish = async (failedValue: addHospitalParamType) => {
    if (id) {
      await updateHosSetFormRequest({ ...failedValue, id });
    } else {
      await addHosSetFormRequest(failedValue);
    }
    message.success(id ? "更新成功" : "添加成功");
    navigate("/syt/hospital/hospitalSet");
  };
  return (
    <div>
      <Card>
        <Form
          name="basic"
          form={form}
          labelCol={{ span: 3 }}
          wrapperCol={{ span: 18 }}
          onFinish={onFinish}
        >
          <Form.Item
            label="医院名称"
            name="hosname"
            rules={[
              {
                required: true,
                transform(value) {
                  return value ? value.trim() : value;
                },
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="医院编号"
            name="hoscode"
            rules={[
              {
                required: true,
                transform(value) {
                  return value ? value.trim() : value;
                },
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="api基础路径"
            name="apiUrl"
            rules={[
              {
                required: true,
                transform(value) {
                  return value ? value.trim() : value;
                },
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="联系人姓名"
            name="contactsName"
            rules={[
              {
                required: true,
                transform(value) {
                  return value ? value.trim() : value;
                },
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="联系人手机"
            name="contactsPhone"
            rules={[
              {
                required: true,
                transform(value) {
                  return value ? value.trim() : value;
                },
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 3 }}>
            <Button type="primary" htmlType="submit" className={style.mr}>
              提交
            </Button>
            <Button
              type="default"
              onClick={() => {
                navigate(-1);
              }}
            >
              返回
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
}
